﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.Serialization;
using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace App
{
    public partial class Administration : System.Web.UI.Page
    {
        bool prerender = true;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Common.GetLoggedUser(this.Page) == null)
            {
                Response.Redirect("Login.aspx");
                return;
            }
            if (MidApp.ManageIsAdministrator.IsAdministrator(Common.GetLoggedUser(this.Page).UserID) == false)
            {
                Response.Redirect("EventList.aspx");
                return;
            }

        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (prerender)
            {
                SetDropDownListEventForTeam();
            }
            AssignTodaysDate();

            RepeaterTeamForums.DataSource = MidApp.ManageTeam.GetAllActiveTeamsOrderedByTeamName();
            RepeaterTeamForums.DataBind();

        }
        private void SetDropDownListEventForTeam()
        {
            var aux = MidApp.ManageTeam.GetAllActiveTeamsOrderedByTeamName();

            foreach (var item in aux)
            {
                DropDownListTeamEvent.Items.Add(item.TeamName);
            }
        }
        public void AssignTodaysDate()
        {
            CalendarExtender1.StartDate = DateTime.Today;
            txtboxFinalDate_CalendarExtender.StartDate = DateTime.Today;
        }
        protected void DropDownListTeamEvent_SelectedIndexChanged(object sender, EventArgs e)
        {
            prerender = false;

        }
        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            prerender = false;
            Panel4.Visible = !Panel4.Visible;
            txtboxFinalDate.Visible = !txtboxFinalDate.Visible;
            Label8.Visible = !Label8.Visible;
            CompareValidator2.Enabled = true;
            RequiredFieldValidator5.Enabled = true;
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            AppLib.Event newevent = new AppLib.Event();

            newevent.EventName = txtboxEventName.Text;
            newevent.Address = txtboxAddress.Text;
            newevent.CreatedBy = Common.GetLoggedUser(this.Page).Nick;
            newevent.Price = null;
            newevent.Repeatable = chckRepeatable.Checked;
            newevent.Description = txtboxDescription.Text;
            newevent.DateOfAnnoucing = DateTime.Now;
            newevent.Seats = null;

            //kontrola zda datum neni v minulosti
            try
            {
                DateTime pom = DateTime.Parse(txtboxEventDate.Text);
                pom = pom.AddHours(double.Parse(DropDownList1.SelectedValue));
                pom = pom.AddMinutes(double.Parse(DropDownList2.SelectedValue));

                if (DateTime.Compare(DateTime.Now, pom) > 0)
                {
                    lblInfo.Text = "Nelze vypsat akci v minulosti";
                    return;
                }
                else
                {
                    newevent.DateOfHolding = pom;
                }
            }
            catch (Exception)
            {
                lblInfo.Text = "Špatný formát data";
                return;
            }

            if (CheckBoxAllTeam.Checked != true)
            {
                if (chckRepeatable.Checked)
                {
                    int span = int.Parse(ListBox1.SelectedItem.Value);
                    newevent.SpanBetweenHoldings = int.Parse(ListBox1.SelectedItem.Value);
                    DateTime dateOfHolding = newevent.DateOfHolding;
                    DateTime dateOfEnding = DateTime.Parse(txtboxFinalDate.Text);
                    dateOfEnding = dateOfEnding.AddHours(23);

                    Stack<AppLib.Event> stack = new Stack<AppLib.Event>();

                    while (dateOfHolding <= dateOfEnding)
                    {
                        AppLib.Event novyEvent = new AppLib.Event();

                        novyEvent.Address = newevent.Address;
                        novyEvent.CreatedBy = newevent.CreatedBy;
                        novyEvent.DateOfAnnoucing = newevent.DateOfAnnoucing;
                        novyEvent.DateOfHolding = dateOfHolding;
                        novyEvent.Description = newevent.Description;
                        novyEvent.EventID = newevent.EventID;
                        novyEvent.EventName = newevent.EventName;
                        novyEvent.Participates = newevent.Participates;
                        novyEvent.Price = null;
                        novyEvent.Repeatable = newevent.Repeatable;
                        novyEvent.Seats = null;
                        novyEvent.SpanBetweenHoldings = newevent.SpanBetweenHoldings;

                        stack.Push(novyEvent);

                        dateOfHolding = dateOfHolding.AddDays(span);
                    }
                    MidApp.ManageEvent.CreateNewEvents(stack, MidApp.ManageTeam.GetTeamByTeamName(DropDownListTeamEvent.SelectedItem.ToString()).TeamID, false);
                    lblInfo.Text = "Akce vytvořena";
                }
                else
                {
                    MidApp.ManageEvent.CreateNewEvent(newevent, MidApp.ManageTeam.GetTeamByTeamName(DropDownListTeamEvent.SelectedItem.ToString()).TeamID, false);
                    lblInfo.Text = "Akce vytvořena";
                }


            }
            else
            {
                if (chckRepeatable.Checked)
                {
                    int span = int.Parse(ListBox1.SelectedItem.Value);
                    newevent.SpanBetweenHoldings = int.Parse(ListBox1.SelectedItem.Value);
                    DateTime dateOfHolding = newevent.DateOfHolding;
                    DateTime dateOfEnding = DateTime.Parse(txtboxFinalDate.Text);
                    dateOfEnding = dateOfEnding.AddHours(23);

                    Stack<AppLib.Event> stack = new Stack<AppLib.Event>();

                        while (dateOfHolding <= dateOfEnding)
                        {
                            AppLib.Event novyEvent = new AppLib.Event();

                            novyEvent.Address = newevent.Address;
                            novyEvent.CreatedBy = newevent.CreatedBy;
                            novyEvent.DateOfAnnoucing = newevent.DateOfAnnoucing;
                            novyEvent.DateOfHolding = dateOfHolding;
                            novyEvent.Description = newevent.Description;
                            novyEvent.EventID = newevent.EventID;
                            novyEvent.EventName = newevent.EventName;
                            novyEvent.Participates = newevent.Participates;
                            novyEvent.Price = null;
                            novyEvent.Repeatable = newevent.Repeatable;
                            novyEvent.Seats = null;
                            novyEvent.SpanBetweenHoldings = newevent.SpanBetweenHoldings;

                            stack.Push(novyEvent);

                            dateOfHolding = dateOfHolding.AddDays(span);
                        }

                    var aux = MidApp.ManageTeam.GetAllActiveTeamsOrderedByTeamName();
                    foreach (var item in aux)
                    {
                        MidApp.ManageEvent.CreateNewEvents(stack, item.TeamID, false);
                    }
                    lblInfo.Text = "Akce vytvořena";

                }
                else
                {
                    var aux = MidApp.ManageTeam.GetAllActiveTeamsOrderedByTeamName();
                    foreach (var item in aux)
                    {                        
                        MidApp.ManageEvent.CreateNewEvent(newevent, item.TeamID, false);  
                    }
                    lblInfo.Text = "Akce vytvořena";
                }
            }
        }

        protected void CheckBoxAllTeam_CheckedChanged(object sender, EventArgs e)
        {
            prerender = false;
            DropDownListTeamEvent.Enabled = !DropDownListTeamEvent.Enabled;
            
        }
        protected void LinkButtonTeam_Click(object sender, EventArgs e)
        {
            Response.Cookies["ThreadList"].Value = ((LinkButton)sender).CommandArgument;
            Response.Redirect("ThreadList.aspx");
        }
        protected void LinkButtonTeamDetail_Click(object sender, EventArgs e)
        {
            Response.Cookies["TeamDetail"].Value = ((LinkButton)sender).CommandArgument;
            Response.Redirect("TeamDetails.aspx");
        }
        protected void LinkButtonEvents_Click(object sender, EventArgs e)
        {
            Response.Cookies["EventAdmin"].Value = ((LinkButton)sender).CommandArgument;
            Response.Redirect("EventsList.aspx");
        }
    }
}